﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using DevExpress.AgDataGrid;
using System.Windows.Media;

namespace Test.Silverlight.DataGrid {
    public partial class Preview : UserControl {
        Canvas globalImageRootCanvas;

        public Preview() {
			InitializeComponent();
			grid.DataSource = CarsData.DataSource;
		}
        void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            SmallCar();
            e.Handled = true;
        }
        void Button_Click(object sender, RoutedEventArgs e) {
            Button btn = (Button)sender;
            btn.Content = "Thank you!";
        }
        Panel GetDemosRootLayout() {
            FrameworkElement root = this;
			while(VisualTreeHelper.GetParent(root) != null) {
				root = (FrameworkElement)VisualTreeHelper.GetParent(root);
            }
            return (Panel)root.FindName("LayoutRoot");
        }
        AgDataGridRow GetRowForRowChild(FrameworkElement child) {
			FrameworkElement parent = (FrameworkElement)VisualTreeHelper.GetParent(child);
            while (!(parent is AgDataGridRow))
				parent = (FrameworkElement)VisualTreeHelper.GetParent(parent);
            return (AgDataGridRow)parent;
        }
        void grid_Loaded(object sender, RoutedEventArgs e) {
            Panel layoutRoot = GetDemosRootLayout();
            if (globalImageRootCanvas == null) {
                globalImageRootCanvas = new Canvas();
                layoutRoot.Children.Add(globalImageRootCanvas);
				((Panel)VisualTreeHelper.GetParent(GlobalImageContainerCanvas)).Children.Remove(GlobalImageContainerCanvas);
                globalImageRootCanvas.Children.Add(GlobalImageContainerCanvas);
            }
        }
        BitmapImage LoadPicture(int rowHandle) {
            BitmapImage bi = new BitmapImage();
            bi.SetSource(new MemoryStream((byte[])grid.GetCellValue(rowHandle, "Picture")));
            return bi;
        }
        void TransmissSpeedCountColumn_PrepareCellDisplayElement(object sender, PrepareCellDisplayElementEventArgs e) {
            string s = ((int)e.CellData.CellValue).ToString();
            s += (((string)((Cars)e.CellData.RowValue).TransmissAutomatic) == "Yes") ? "AT" : "MT";
            ((TextBlock)e.DisplayElement).Text = s;
        }

        void grid_PreviewStatus(object sender, PreviewStatusEventArgs e) {
            if (!e.IsOpening || Parent == null) return;
            FrameworkElement parent = (FrameworkElement)Parent;
			if(parent != null)
				while(VisualTreeHelper.GetParent(parent) != null) {
					parent = (FrameworkElement)VisualTreeHelper.GetParent(parent);
				}
            Button button = ((Button)((FrameworkElement)e.Preview).FindName("orderNowButton"));
            if (button.Style == null)
                button.Style = ((MainPage)parent).DefaultButtonStyle;
            ScrollViewer scrollViewer = (ScrollViewer)((FrameworkElement)e.Preview).FindName("scrollViewer");
            if (scrollViewer.Style == null) {
                scrollViewer.Style = this.owner.ScrollViewerStyle;
            }
        }
        void Icon_MouseLeave(object sender, MouseEventArgs e) {
            SmallCar();
            //e.Handled = true; //BC
        }
        void Icon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
			Image img = (Image)sender;
			GlobalImage.Source = LoadPicture(GetRowForRowChild(img).Handle);
			Point location = img.TransformToVisual(globalImageRootCanvas).Transform(new Point());
			GlobalImageContainerCanvas.SetValue(Canvas.LeftProperty, location.X);
			GlobalImageContainerCanvas.SetValue(Canvas.TopProperty, location.Y);
			GlobalImageContainer.SetValue(Canvas.LeftProperty, 0.0);
			GlobalImageContainer.SetValue(Canvas.TopProperty, 0.0);
			GlobalImageContainer.IsHitTestVisible = true;

			Storyboard storyboard = GlobalImageContainerCanvas.Resources["Large"] as Storyboard;
			if(storyboard != null) {
				try {
					storyboard.Begin();
				} catch(Exception) {
				}
			}
			e.Handled = true;
		}
		void SmallCar() {
			Storyboard storyboard = GlobalImageContainerCanvas.Resources["Small"] as Storyboard;
			if(storyboard != null) {
				try {
					storyboard.Begin();
				} catch(Exception) {
				}
			}
			GlobalImageContainer.IsHitTestVisible = false;
		}

        #region OptionsPanel
        void rb_Checked(object sender, RoutedEventArgs e) {
            grid.PreviewVisibility = GetDataPreviewVisibilityForRadioButton((RadioButton)sender);
        }
        #endregion
    }
}
